<?php

namespace App\Kuafu\Banner\Controller\B;

use App\Kuafu\Banner\Exceptions\BannerException;
use App\Kuafu\Banner\Validate\BannerConfigValidate;
use App\Kuafu\Banner\Enum\BannerCodeEnum;
use App\Kuafu\Banner\Service\BannerConfigService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class BannerConfigController extends BaseController
{
    /**
     * 根据id获取详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BannerException
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public function getBannerConfigById(Request $request, int $id): array
    {
        $id    = (int)$id;
        $valid = BannerConfigValidate::getBannerById(['id' => $id]);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_NOT_INPUT, '非法数据，参数校验失败');
        }
        $info = BannerConfigService::getBannerById($id);

        return $this->success($info);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getBannerConfigList(Request $request)
    {
        $pageSize = $request->get('pageSize', 5);
        $pageNum  = $request->get('pageNum', 1);

        $bannerConfigList = BannerConfigService::getBannerList($pageSize, $pageNum);
        return $this->success(['bannerConfigList' => $bannerConfigList]);
    }

    /**
     * 横幅位置添加
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BannerException
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public function addBannerConfig(Request $request)
    {
        $valid = BannerConfigValidate::addBannerConfig($request);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_NOT_INPUT, $valid);
        }

        BannerConfigService::addBannerConfig($request->all());
        return $this->success();
    }

    /**
     * 删除（软删除）
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws BannerException
     */
    public function delBannerConfig(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = BannerConfigValidate::delBannerConfig(['id' => $id]);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_NOT_INPUT, '非法数据，参数校验失败');
        }

        BannerConfigService::delBanner($id);
        return $this->success();
    }

    /**
     * 根据id修改信息
     * @param Request $request
     * @return JsonResponse
     * @throws BannerException
     */
    public function setBannerConfig(Request $request): JsonResponse
    {
        $valid = BannerConfigValidate::setBannerConfig($request);

        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_NOT_INPUT, '非法数据，参数校验失败');
        }
        BannerConfigService::setBannerConfig($request);
        return $this->success();
    }

}
